package com.marktotop.utils;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileOutputStream;

/**
 * 创建dom4j的工具类
 *      对于dom4j来说，操作XML文件只有读写操作
 */
public class Dom4jUtil {

    // 解析XML
    public static Document getDocument(String path) throws Exception {
        // 创建解析器
        SAXReader sr = new SAXReader();
        // 得到document
        Document doc = sr.read(path);
        return doc;
    }

    // 回写xml
    public static void ReWrite(String path, Document doc) throws Exception {
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path),
                new OutputFormat().createPrettyPrint());
        xmlWriter.write(doc);
        xmlWriter.close();
    }

}
